package olga.gorbatiuk.journal.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import olga.gorbatiuk.journal.Journal;
import olga.gorbatiuk.record.Record;
import olga.gorbatiuk.record.comparators.ByDateAndImportanceComparator;
import olga.gorbatiuk.record.comparators.ByDateComparator;

public class CollectionJournal implements Journal {
    private List<Record> recordList;

    // private int size;
    // private int quantity;

    public CollectionJournal() {
	// this.size = 0;
	this.recordList = new ArrayList<Record>();
    }

    public CollectionJournal(CollectionJournal journal) {
	// this.size = journal.getSize();
	List<Record> newRecordList = new ArrayList<Record>();
	Collections.copy(newRecordList, journal.getRecordList());
	this.recordList = newRecordList;
    }

    @Override
    public void add(Record r) {
	recordList.add(r);
    }

    @Override
    public void add(Journal j) {
	if (j.getClass() == this.getClass()) {
	    CollectionJournal otherCollectionJournal = (CollectionJournal) j;
	    recordList.addAll(otherCollectionJournal.getRecordList());
	}
    }

    @Override
    public void remove(Record r) {
	recordList.remove(r);

    }

    @Override
    public Record get(int index) {
	return recordList.get(index);
    }

    @Override
    public void set(int index, Record record) {
	recordList.set(index, record);

    }

    @Override
    public void insert(int index, Record record) {
	recordList.add(index, record);

    }

    @Override
    public void remove(int index) {
	recordList.remove(index);

    }

    @Override
    public void remove(int fromIndex, int toIndex) {
	// TODO Auto-generated method stub

	Iterator<Record> iterator = recordList.iterator();
	for (int i = fromIndex; i < toIndex; i++) {

	}
	// recordList.r
    }

    @Override
    public void removeAll() {
	recordList.removeAll(recordList);
    }

    @Override
    public int size() {
	return recordList.size();
    }

    @Override
    public Journal filter(String s) {
	Journal newJournal = new CollectionJournal();
	for (Record record : recordList) {
	    if (record.toString().equals(s)) {
		newJournal.add(record);
	    }
	}
	return newJournal;
    }

    @Override
    public Journal filter(Date fromDate, Date toDate) {
	// TODO Auto-generated method stub
	// есть ли какой-то джавовский метод для подобной фильтрации?

	return null;
    }

    @Override
    public void sortByDate() {
	Collections.sort(recordList, new ByDateComparator());
    }

    @Override
    public void sortByImportanceDate() {
	Collections.sort(recordList, new ByDateAndImportanceComparator());

    }

    @Override
    public void sortByImportanceSourceDate() {
	// TODO Auto-generated method stub

    }

    @Override
    public void sortBySourceDate() {
	// TODO Auto-generated method stub

    }

    @Override
    public void printRecords() {
	for (Record record : recordList) {
	    System.out.println(record.toString());
	}
    }

    public List<Record> getRecordList() {
	return recordList;
    }

    public void setRecordList(List<Record> recordList) {
	this.recordList = recordList;
    }

    // public int getSize() {
    // return size;
    // }
    //
    // public void setSize(int size) {
    // this.size = size;
    // }
    //
    // public int getQuantity() {
    // return quantity;
    // }

}
